package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.dao.IAudienceRatingDao;
import com.magicode.cms.dao.IChannelDao;
import com.magicode.cms.dao.IProgramAudienceRatingDao;
import com.magicode.cms.entiy.base.Article;
import com.magicode.cms.entiy.base.AudienceRating;
import com.magicode.cms.entiy.base.BroadPlatformAudienceRating;
import com.magicode.cms.entiy.base.Channel;
import com.magicode.cms.entiy.base.News;
import com.magicode.cms.entiy.base.ProgramAudienceRating;
import com.magicode.cms.service.IArticleService;
import com.magicode.cms.service.IAudienceRatingService;
import com.magicode.cms.service.IChannelService;
import com.magicode.cms.service.IProgramAudienceRatingService;
import com.magicode.common.exception.BusinessException;

@Service("serviceProgramAudienceRating")
public class ProgramAudienceRatingServiceImpl extends BaseServiceImpl<ProgramAudienceRating,IProgramAudienceRatingDao> implements IProgramAudienceRatingService,UtilPath{

	@Autowired
	@Qualifier("daoProgramAudienceRating")
	private IProgramAudienceRatingDao programAudienceRatingDao;


	public IProgramAudienceRatingDao getProgramAudienceRatingDao() {
		return programAudienceRatingDao;
	}

	public void setProgramAudienceRatingDao(
			IProgramAudienceRatingDao programAudienceRatingDao) {
		this.programAudienceRatingDao = programAudienceRatingDao;
	}

	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "programName");
		map.put(json_title, "名称（节目/电视剧）");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "channelName");
		map.put(json_title, "频道");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "type");
		map.put(json_title, "子榜单");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "statisticsDate");
		map.put(json_title, "日期");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "rating");
		map.put(json_title, "收视率");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "quotas");
		map.put(json_title, "收视份额");
		map.put("width", 50);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "source");
		map.put(json_title, "样本类型");
		map.put("width", 50);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "status");
		map.put(json_title, "状态");
		map.put("width", 50);
		beaninfo.add(map);
		return beaninfo;
	}

	@Override
	public String getHql(Map<String, String> querywhere) throws Exception {
		String hql ="from " +ProgramAudienceRating.class.getName()+" "+entity_alias_name+" ";

		hql += " left join "+entity_alias_name+".type as ty ";
		hql += " left join "+entity_alias_name+".source as s ";
		hql += " left join "+entity_alias_name+".author as au ";
		hql += " left join "+entity_alias_name+".program as pr ";
		hql += " left join "+entity_alias_name+".platform as pl ";
		hql += " left join "+entity_alias_name+".sampleType as st ";
		
		if(null!=querywhere && !querywhere.isEmpty()){
			hql += "where 1 = 1  ";  //and "+entity_alias_name+".isContribute = 0
			if(querywhere.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id = '"+querywhere.get(field_id)+"'";
			}
			if(querywhere.containsKey("title")){
				hql = hql+" and "+entity_alias_name+".programName like '%"+querywhere.get("title")+"%'";
			}
			if(querywhere.containsKey("type")){
				hql = hql+" and "+entity_alias_name+".type.id = '"+querywhere.get("type")+"'";
			}
//			if(querywhere.containsKey("beginDate")){
//				hql = hql+" and "+entity_alias_name+".statisticsDate >= '"+querywhere.get("beginDate")+"'";
//			}
			if(querywhere.containsKey("beginDate")){
				hql = hql+" and "+entity_alias_name+".statisticsDate = '"+querywhere.get("beginDate")+"'";
			}
			if(querywhere.containsKey("endDate")){
				hql = hql+" and "+entity_alias_name+".statisticsDate <= '"+querywhere.get("endDate")+"'";
			}
			if(querywhere.containsKey("source")){
				hql = hql+" and "+entity_alias_name+".source.title like '%"+querywhere.get("source")+"%'";
			}
			if(querywhere.containsKey("channel")){
				hql = hql+" and "+entity_alias_name+".channelName like '%"+querywhere.get("channel")+"%'";
			}
			if(querywhere.containsKey("status")){
				hql = hql+" and "+entity_alias_name+".status ='"+querywhere.get("status")+"'";
			}
		}
		return hql;
	}

	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			hql += " order by t.statisticsDate desc , t.createTime desc ";
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put("programName", "pr.titleCn as programName ");
			gridsel.put("channelName", "pl.titleCn as channelName ");
			gridsel.put("statisticsDate", sql_date_method+"("+entity_alias_name+".statisticsDate  ,'"+sql_date_format+"' ) as statisticsDate ");
			gridsel.put("rating", ""+entity_alias_name+".rating as rating ");
			gridsel.put("quotas",""+entity_alias_name+".quotas as quotas ");
//			gridsel.put("type", "case when "+entity_alias_name+".type = 1 then '资讯' else '视频' end as type ");
			gridsel.put("type", "ty.title as type ");
			gridsel.put("source", "st.title as source ");
			gridsel.put("status", "case when "+entity_alias_name+".status = 0 then '草稿' "
					+ " when "+entity_alias_name+".status = 1 then '待审核' "
					+ " when "+entity_alias_name+".status = 2 then '已发布'  end as status ");
//			gridsel.put("shareCommentsCount", ""+entity_alias_name+".shareCount || '|' || "+entity_alias_name+".commentsCount "
//					+ "  as shareCommentsCount ");
			
			return this.queryMapDataList(hql, gridsel, start, size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}

	@Override
	public List<ProgramAudienceRating> getAudienceRatingByBroadPlatform(String Id) throws Exception {
		Map<String,String> map = new HashMap<String,String> ();
		map.put("id", Id);
		String hql = " from "+ProgramAudienceRating.class.getName()+" t  left outer join fetch t.platform as pf "
				+ "where pf.id ='"+map.get("id")+"'";
		List<ProgramAudienceRating> user =  programAudienceRatingDao.getCommonDaoService().queryList(hql, 0, 0, null);
		return user;
	}
	
	@Override
	public List<ProgramAudienceRating> getAudienceRatingByProgram(String Id) throws Exception {
		Map<String,String> map = new HashMap<String,String> ();
		map.put("id", Id);
		String hql = " from "+ProgramAudienceRating.class.getName()+" t  left outer join fetch t.program as pf "
				+ "where pf.id ='"+map.get("id")+"'";
		List<ProgramAudienceRating> user =  programAudienceRatingDao.getCommonDaoService().queryList(hql, 0, 0, null);
		return user;
	}
	@Override
	public IProgramAudienceRatingDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return programAudienceRatingDao;
	}

	
}
